<?php
/**
 *
 * @author Jean-Baptiste
 * @version 
 */
/**
 * Rewrite Action Helper
 *
 * @uses actionHelper Zend_Controller_Action_Helper
 */
class HAE_Helper_Rewrite extends Zend_Controller_Action_Helper_Abstract
{
    /**
     *
     * @var Zend_Loader_PluginLoader
     */
    public $pluginLoader;
    /**
     * Constructor: initialize plugin loader
     *
     * @return void
     */
    public function __construct ()
    {
        $this->pluginLoader = new Zend_Loader_PluginLoader();
    }
    
    function getRewrite($string=null)
    {
    	// return if empty
    	if(empty($string)) return false;
    
    	// replace spaces by "-"
    	// convert accents to html entities
    	$string=htmlentities(utf8_decode(str_replace(' ', '-', $string)));
    
    	// remove the accent from the letter
    	$string=preg_replace(array('@&([a-zA-Z]){1,2}(acute|grave|circ|tilde|uml|ring|elig|zlig|slash|cedil|strok|lig){1};@', '@&[euro]{1};@'), array('${1}', 'E'), $string);
    
    	// now, everything but alphanumeric and -_ can be removed
    	// aso remove double dashes
    	$string=preg_replace(array('@[^a-zA-Z0-9\-_]@', '@[\-]{2,}@'), array('', '-'), html_entity_decode($string));
    	
    	// Lowercase everything
    	$string= strtolower($string);
    	
    	return $string;
    }
    /**
     * Strategy pattern: call helper as broker method
     */
    public function direct ($string)
    {
        return $this->getRewrite($string);
    }
    
    
}
